<template>
  <div class="user-tel">
    <el-page-header @back="goBack" content="修改手机号"></el-page-header>
    <!-- 修改手机号 -->
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="新手机号" prop="phone">
        <el-input v-model="ruleForm.phone" placeholder="请输入新手机号" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item label="验证码" prop="code">
          <el-input v-model="ruleForm.code" placeholder="请输入验证码">
            <template slot="append">
              <el-button v-no-more-click:3000
                :disabled="btnIsDisabled"
                class="btn-code"
                type="primary"
                @click="getVCode()">
                {{VCodeText}}
              </el-button>
            </template>

          </el-input>
      </el-form-item>
      <el-form-item>
        <el-button v-no-more-click:3000 type="primary" @click="changePhone('ruleForm')">更换手机号</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: "updateTel",
  data() {
    return {
      ruleForm: {},
      rules: {
        phone: [
          { required: true, message: "请输入正确手机号", trigger: "blur" },
          { min: 11, max: 11, message: "请输入11位手机号", trigger: "blur" }
        ],
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }]
      },
      VCodeText: "获取验证码",
      btnIsDisabled: false
    };
  },
  created () {
    if(this.$store.state.isUpdateTel == false){
      this.$router.push('/user/set/verifyPhone')
    }
  },
  methods: {
    goBack() {
      this.$router.go(-2);
    },
    //验证码
    getVCode() {
      let that = this;
      if (/^1[3456789]\d{9}$/.test(that.ruleForm.phone)) {
        let num = 60;
        that.btnIsDisabled = true;
        let timer = setInterval(function() {
          that.VCodeText = num + "秒后可重新发送";
          num -= 1;
          if (num < 0) {
            that.VCodeText = "获取验证码";
            that.btnIsDisabled = false;
            clearInterval(timer);
          }
        }, 1000);
        that.$axios
          .post(
            "/api/captcha",{
              phone: that.ruleForm.phone,
              type: 1
            }
          )
          .then(res => {
            if (res.data.code == 200) {
              that.$message({
                message: "验证码发送成功！",
                type: "success"
              });
            } else {
              if (res.data.code == 400) {
                that.$message({
                  message: res.data.message,
                  type: "error"
                });
              } else {
                that.$message({
                  message: "验证码发送失败！",
                  type: "error"
                });
              }
            }
          })
          .catch(err => {
            this.$message.error("获取验证码失败", err);
          });
      } else {
        that.$message({
          message: "手机号不能为空且格式不能有误",
          type: "warning"
        })
      }
    },
    // 更换新手机号
    changePhone(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$axios
            .put(
              "api/consumer/changePhone",{
                phone: this.ruleForm.phone,
                code: this.ruleForm.code
              }
            )
            .then(res => {
              if (res.data.code == 200) {
                this.$message.success("手机号码更换成功");
                //this.$store.commit("delUser", "");
                //this.$axios.defaults.headers.common["Authorization"] = "";
                this.$router.replace({
                	path: "/user/myOrder"
                })
              }
            })
            .catch(err => {
              this.$message.error(err)
            });
        } else {
          this.$message.error("请按要求填写手机号码及验证码");
          return false;
        }
      });
    }
  }
};
</script>
<style lang="less">
.user-tel {
  .el-form {
    width: 800px;
    margin: 80px 0 0 50px;
		.el-input {
		  width: 300px;
		}
    .btn-code {
      float: right;
      padding: 13px 20px ;
      span {
        min-width: 115px;
        display: block;
      }
    }
  }
}
// .el-button {
//   padding: 13px 20px;
// }
</style>